package delegated_property

import kotlin.reflect.KProperty

//属性代理 每一次赋值和获取值时进行修改和获取

class Example {
    var p: String by Delegate()
    var d: String by Delegate()

    override fun toString() = "Example Class"
}

class Delegate {
    var ppp="12"
    operator fun getValue(ref:Any?,prop:KProperty<*>) :String{
        return "hello $ppp "
    }

    operator fun setValue(ref:Any?,prop:KProperty<*>,value:String){
        ppp+=value
        println( "hello $ppp")
    }

}

fun main(args: Array<String>) {
    val e = Example()
    println(e.p)
    println(e.d)

    e.p = "NEW"
    e.d = "NEW1"
    println(e.d)
}